package com.zlp.partice.io.bio.file;



import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.rmi.RemoteException;

/**
 * @author: zhulaiping
 * @time: 2022/8/8 10:45
 */
public class BioDemo1_Client {

    public static void main(String[] args) throws IOException {
        InetAddress localhost = InetAddress.getByName("localhost");
        Socket socket = new Socket(localhost, 9999);
        //DataOutputStream dataOutputStream = new DataOutputStream();  可以分段发送
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\io\\1111.jpg"));
        OutputStream out = socket.getOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = bis.read(buffer))!=-1){
            out.write(buffer,0,len);
        }
        socket.shutdownOutput();
        BufferedInputStream response = new BufferedInputStream(socket.getInputStream());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((len = response.read(buffer))!=-1){
            baos.write(buffer,0,len);
        }
        if(baos.toByteArray().length==0){
            throw new RuntimeException("文件上传失败！");
        }
        System.out.println(baos.toString());

    }
}
